const { think } = require('thinkjs');
const Base = require('./base.js');
const rp = require('request-promise');

module.exports = class extends Base {
  async monthAction() {
    const page = this.get('page') || 1;
    const size = this.get('size') || 20;

    const query = this.get();

    query.type = 'staff';

    delete query.page;
    delete query.size;
    const data = await this.model('output_list')
      .where(query)
      .order(['id DESC'])
      .page(page, size)
      .countSelect();

    return this.success(data);
  }
  async royaltyAction() {
    const id = this.get('id');
    // query.status = this.get('status') || 0
    if (think.isEmpty(id)) {
      return this.fail(401, '找不到服务');
    }

    const query = {
      staff_id: id,
    };

    const data = await this.model('output_list')
      .where(query)
      .order(['id DESC'])
      .select();

    for (const item of data) {
      if (item.royalty_type === 1) {
        item.project_id = [
          item.project_id,
          item.building_id,
          item.floor_id,
          item.room_id,
        ];
      } else {
        item.project_id = null;
      }
    }

    return this.success(data);
  }
  async putAction() {
    const values = this.post();
    const id = this.post('id');
    const model = this.model('output_list');

    if (id > 0) {
      delete values.id;
      await model.where({ id: id }).update(values);
      this.model('addLog').add({
        user_id: this.getLoginUserId(),
        title: '编辑员工工资',
        content: id,
      });
    } else {
      // const
      // values.create_time = ['exp', 'CURRENT_TIMESTAMP()'];
      // await model.add(values);
    }
    return this.success(values);
  }

  async putRoyaltyAction() {
    const values = this.post();
    const id = this.post('id');
    const model = this.model('output_list');

    if (values.royalty_type === 1) {
      const projectInfo = values.project_id;
      values.project_id = projectInfo[0];
      values.building_id = projectInfo[1];
      values.floor_id = projectInfo[2];
      values.room_id = projectInfo[3];
    } else {
      values.project_id = null;
      values.building_id = null;
      values.floor_id = null;
      values.room_id = null;
    }

    if (id > 0) {
      delete values.id;
      await model.where({ id: id }).update(values);
      this.model('addLog').add({
        user_id: this.getLoginUserId(),
        title: '编辑员工提成',
        content: id,
      });
    } else {
      // const staff = await this.model('output_list').where({id: values.staff_id}).find()

      values.create_time = ['exp', 'CURRENT_TIMESTAMP()'];
      values.type = 'royalty';
      const addId = await model.add(values);
      this.model('addLog').add({
        user_id: this.getLoginUserId(),
        title: '新增员工提成',
        content: addId,
      });
    }
    return this.success(values);
  }
  async delAction() {
    const id = this.get('id');
    const d = await this.model('output_list')
      .where({ id: id })
      .find();
    await this.model('output_list')
      .where({ id: id })
      .limit(1)
      .delete();

    this.model('addLog').add({
      user_id: this.getLoginUserId(),
      title: '删除员工提成',
      content: JSON.stringify(d),
    });

    return this.success();
  }
};
